<template>
  <div class="mod-config">
    <basic-container>
      <el-divider content-position="left">查询条件</el-divider>
      <el-form inline size="mini" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="收货日期">
              <el-col :span="11">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="dataForm.receiveTimeStart"
                  value-format="yyyy-MM-dd"
                  style="width: 100%;"
                ></el-date-picker>
              </el-col>
              <el-col class="line" :span="2" style="text-align: center;">至</el-col>
              <el-col :span="11">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="dataForm.receiveTimeEnd"
                  value-format="yyyy-MM-dd"
                  style="width: 100%;"
                ></el-date-picker>
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="商品代码">
              <el-input size="mini" v-model="dataForm.goodsCode" style="width: 175px;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="商品名称">
              <el-input size="mini" v-model="dataForm.goodsName" style="width: 175px;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
                <el-row>
          <el-col :span="8">
            <el-form-item label="商品类别">
              <el-select v-model="dataForm.ctgId" @change="changeCtgId" clearable>
                <el-option v-for="item in ctgOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="制造商">
              <el-input v-model="dataForm.supplyName" @change="changeSupplyId">
                <el-button slot="append" @click="openSupplyDialog">选择</el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品品种" prop="brandName">
              <el-input v-model="dataForm.brandName" readonly="readonly">
                <el-button slot="append" @click="getBrandList">选择</el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="调拨类型">
              <el-radio-group v-model="dataForm.type" @change="typeChangeHandle">
                <el-radio label="1">发出</el-radio>
                <el-radio label="2">收入</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="统计类型">
              <el-radio v-model="dataForm.statisticalType" label="1">品牌</el-radio>
              <el-radio v-model="dataForm.statisticalType" label="2">商品</el-radio>
              <el-radio v-model="dataForm.statisticalType" label="3">条码</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="调拨成本">
              <el-radio v-model="dataForm.priceType" label="4">0.7成本</el-radio>
              <el-radio v-model="dataForm.priceType" label="1">含税成本</el-radio>
              <el-radio v-model="dataForm.priceType" label="2">不含税成本</el-radio>
              <el-radio v-model="dataForm.priceType" label="3">加权平均成本</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="dataForm.type == '1'">
          <el-col :span="12">
            <el-form-item label="发出仓位">
              <el-select size="mini" v-model="outStockId" clearable @change="outStockChangeHandle">
                <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!--<el-col :span="12"  v-else>
            <el-form-item label="发出仓位">
              <el-select size="mini" v-model="outStockId"  @change="outStockChangeHandle" disabled>
                <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>-->
          <el-col :span="12">
            <el-form-item label="接收仓位">
              <el-input size="mini" type="textarea" v-model="dataForm.inStockName" readonly>
              </el-input>
            </el-form-item>
            <el-form-item label-width="0">
              <el-button size="mini" @click="getStockList">选择</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="dataForm.type == '2'">
          <el-col :span="12">
            <el-form-item label="发出仓位">
              <el-input size="mini" type="textarea" v-model="dataForm.outStockName" readonly>
              </el-input>
            </el-form-item>
            <el-form-item label-width="0">
              <el-button size="mini" @click="getStockList">选择</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接收仓位">
              <el-select size="mini" v-model="inStockId" clearable @change="inStockChangeHandle">
                <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-top: 10px;">
          <el-form-item label-width="5px">

            <el-link :href="printUrl" target="_blank" type="primary" :disabled="isDisabled">
              <el-button size="mini" type="primary" @click="getAllocationInfoList">查 询</el-button>
            </el-link>
          </el-form-item>
        </el-row>
      </el-form>
      <search-stock v-if="stockVisible" ref="stockInfo" @refreshData="getStockInfoList"></search-stock>
      <!-- 弹窗，查询制造商 -->
      <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyData"></search-supply>
      <!-- 弹窗，查询商品品种 -->
      <select-brand v-if="brandVisible" ref="selectBrand" @refreshData="updateFormBrand"></select-brand>
    </basic-container>
  </div>
</template>

<script>
let params = "";
import { filterParams, getDateFormat } from "@/util/tools"
import { mapGetters } from "Vuex"
import { getUserInfo } from "@/api/admin/user"
import { getStockList ,getStockInfo} from '@/api/eims/stock'
import searchStock from "@/components/business/stock-search"
import SearchSupply from '@/components/business/SearchSupply'
import SelectBrand from '../storage/SearchBrand'
import { getCtgList } from '@/api/eims/ctg'


export default {
  data() {
    return {
      printUrl: "",
      isDisabled: true,
      outStockId: "",
      inStockId: "",
      dataForm: {
        receiveTimeStart: "",
        receiveTimeEnd: "",
        type: "1",
        statisticalType: "1",
        goodsCode: "",
        goodsName: "",
        priceType: "4",
        outStockName: "",
        outStockIds: [],
        inStockName: "",
        inStockIds: [],
        supplyName: '',
        supplyId: '',
        brandName: '',
        brandId: ''
      },
      deptOptions: [],
      stockOptions: [],
      ctgOptions: [],
      supplyVisible: false,
      brandVisible: false,
      stockVisible: false
    };
  },
  computed: {
    ...mapGetters(["userInfo", "bizData"])
  },
  components: {
    searchStock,
    SearchSupply,
    SelectBrand
  },
  created() {
    this.getStockOption()
    //this.getStockName()
    this.getGoodsCtgList();
  },
  methods: {
      getStockName(){
          let deptId = this.userInfo.lessDeptId
          getStockInfo(deptId).then(result=>{
              this.outStockId = result.data.data[0].id;
          })
      },
    typeChangeHandle() {
      this.outStockId = ''
      this.inStockId = ''
      this.dataForm.outStockIds = []
      this.dataForm.outStockName = ''
      this.dataForm.inStockIds = []
      this.dataForm.inStockName = ''
    },
    outStockChangeHandle(val) {
      this.stockOptions.forEach(item => {
        this.dataForm.outStockIds = []
        if(item.id == val){
          this.dataForm.outStockName = item.name
        }

      })
        this.dataForm.outStockIds.push(val)
    },
    inStockChangeHandle(val) {
      this.stockOptions.forEach(item => {
        this.dataForm.inStockIds = []
        if(item.id == val){
          this.dataForm.inStockName = item.name
        }

      })
        this.dataForm.inStockIds.push(val)
    },
    getAllocationInfoList() {
      if(!this.dataForm.receiveTimeStart){
        this.$message.warning("请出入查询日期范围");
        return
      }
      if(!this.dataForm.receiveTimeEnd){
        this.$message.warning("请出入查询日期范围");
        return
      }
      if(!this.dataForm.outStockIds.length){
        this.$message.warning('请选择发出仓位')
        return
      }
      if(!this.dataForm.inStockIds.length){
        this.$message.warning('请选择接收仓位')
        return
      }
      this.isDisabled = false
      if(this.dataForm.type ==1){
        if(this.dataForm.statisticalType ==1){
          params = "/allocation_brand_out.cpt";
        }else if(this.dataForm.statisticalType ==2){
          params = "/allocation_goods_out.cpt";
        }else if(this.dataForm.statisticalType ==3){
          params = "/allocation_barcode_out.cpt";
        }
      }else if(this.dataForm.type ==2){
        if(this.dataForm.statisticalType ==1){
          params = "/allocation_brand_in.cpt";
        }else if(this.dataForm.statisticalType ==2){
          params = "/allocation_goods_in.cpt";
        }else if(this.dataForm.statisticalType ==3){
          params = "/allocation_barcode_in.cpt";
        }
      }
      let data = filterParams(this.dataForm);
      let parameter = "";
      for (let key in data) {
        parameter += "&" + key + "=" + this.dataForm[key];
      }
      this.printUrl = this.bizData.printUrl + params + parameter;

    },
    //根据工号获取员工真实名称
    getRealName(workNumber) {
      if (workNumber == "") {
        this.dataForm.createUserName = "";
        this.dataForm.createUser = "";
      } else {
        getUserInfo(workNumber).then(result => {
          if (result.data.data) {
            this.dataForm.createUserName = result.data.data.realName;
            this.dataForm.createUser = result.data.data.id;
          } else {
            this.$message.warning("请输入正确的员工工号");
          }
        });
      }
    },
    getStockList() {
      this.stockVisible = true;
      let data = [];
      if(this.dataForm.type == '1'){
          if (this.dataForm.inStockIds) {
              data = this.dataForm.inStockIds;
          }
      }else if(this.dataForm.type == '2'){
          if (this.dataForm.outStockIds) {
              data = this.dataForm.outStockIds;
          }
      }
      this.$nextTick(() => {
        this.$refs.stockInfo.init(data);
      });
    },
    getStockInfoList(data) {
      let name = "";
      if(this.dataForm.type == '1'){
          this.dataForm.inStockIds = []
        }else if(this.dataForm.type == '2'){     
          this.dataForm.outStockIds = []
        }
      data.forEach(result => {
        if(this.dataForm.type == '1'){
          this.dataForm.inStockIds.push(result.id)
        }else if(this.dataForm.type == '2'){     
          this.dataForm.outStockIds.push(result.id)
        }
        name = name ? name + "," + result.name : result.name;
      });
      if(this.dataForm.type == '1'){
        this.dataForm.inStockName = name
      }else if(this.dataForm.type == '2'){
        this.dataForm.outStockName = name
      }
    },
    getStockOption(){
      getStockList().then(result => {
        this.stockOptions = result.data.data
      })
    },
    //  获取商品类别
    getGoodsCtgList() {
      getCtgList().then(result => {
        this.ctgOptions = result.data.data
      })
    },
    changeCtgId(){
      this.dataForm.supplyId = ''
      this.dataForm.supplyName = ''
      this.dataForm.brandId = ''
      this.dataForm.brandName= ''
    },
    //  打开选择制造商弹窗
    openSupplyDialog() {
      if (!this.dataForm.ctgId) {
        this.$message.warning('请选择商品类别')
        return false
      }
      let data = {
        ctgId: this.dataForm.ctgId
      }
      this.supplyVisible = true
      this.$nextTick(() => {
        this.$refs.supplyDialog.init(data)
      })
    },
    getSupplyData(data){
      this.$set(this.dataForm,'supplyId',data.id)
      this.$set(this.dataForm,'supplyName',data.name)
    },
    changeSupplyId(){
      this.dataForm.brandId = ''
      this.dataForm.brandName= ''
    },
    // 获取商品品种信息
    getBrandList() {
      if (!this.dataForm.ctgId) {
        this.$message.warning('请选择商品类别')
        return false
      }
      if (!this.dataForm.supplyId) {
        this.$message.warning('请选择制造商')
        return false
      }
      let data = {
        ctgId: this.dataForm.ctgId,
        supplyId: this.dataForm.supplyId
      }
      this.brandVisible = true
      this.$nextTick(() => {
        this.$refs.selectBrand.init(data)
      })
    },
    //  更新表单品种信息
    updateFormBrand(data) {
      console.log(data)
      this.dataForm.brandId = data.id
      this.dataForm.brandName = data.name
      this.dataForm.brandCode = data.code
      this.dataForm.name = data.name
      this.dataForm.unitType = data.unitType
    },
  }
};
</script>

<style lang="scss" scoped>
.row-bg {
  background-color: #cadee8;
  // padding-left: 15px;
  padding-right: 15px;
  padding-top: 5px;
  margin-bottom: 2px;
}
</style>
